using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class GameObjectExtensions
{
    public static void SetLayer(this GameObject go, int layer, bool recursive = false)
    {
        if (!recursive)
        {
            go.layer = layer;
            return;
        }

        Queue<Transform> childs = new Queue<Transform>();
        childs.Enqueue(go.transform);
        while(childs.Count > 0)
        {
            var child = childs.Dequeue();
            for(int i=0; i<child.childCount; i++)
            {
                childs.Enqueue(child.GetChild(i));
            }
            child.gameObject.layer = layer;
        }
    }

    public static void SetLayer(this GameObject go, string name, bool recursive = false)
    {
        var layer = LayerMask.NameToLayer(name);
        go.SetLayer(layer, recursive);
    }
}
